﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;


public class HeaderGroupBox : GroupBox
{
    public HeaderGroupBox()
        : base()
    {

    }

    protected override void OnPaint(PaintEventArgs e)
    {
        StringFormat format = new StringFormat();
        format.Trimming = StringTrimming.Character;
        format.Alignment = StringAlignment.Near;
        if ((this.RightToLeft == RightToLeft.Yes))
        {
            format.FormatFlags = (format.FormatFlags | StringFormatFlags.DirectionRightToLeft);
        }
        SizeF stringSize = e.Graphics.MeasureString(Text, Font, ClientRectangle.Size, format);
        if (Enabled)
        {
            Brush br = new SolidBrush(ForeColor);
            e.Graphics.DrawString(Text, Font, br, ClientRectangle, format);
            br.Dispose();
        }
        else
        {
            ControlPaint.DrawStringDisabled(e.Graphics, Text, Font, BackColor, ClientRectangle, format);
        }
        Pen forePen = new Pen(ControlPaint.LightLight(BackColor), SystemInformation.BorderSize.Height);
        Pen forePenDark = new Pen(ControlPaint.Dark(BackColor), SystemInformation.BorderSize.Height);
        Point lineLeft = new Point(ClientRectangle.Left, (ClientRectangle.Top + Convert.ToInt32((Font.Height / 2f))));
        Point lineRight = new Point(ClientRectangle.Right, (ClientRectangle.Top + Convert.ToInt32((Font.Height / 2f))));
        if ((this.RightToLeft != RightToLeft.Yes))
        {
            lineLeft.X = (lineLeft.X + Convert.ToInt32(stringSize.Width));
        }
        else
        {
            lineRight.X = (lineRight.X - Convert.ToInt32(stringSize.Width));
        }
        if ((FlatStyle == FlatStyle.Flat))
        {
            e.Graphics.DrawLine(forePenDark, lineLeft, lineRight);
        }
        else
        {
            e.Graphics.DrawLine(forePenDark, lineLeft, lineRight);
            lineLeft.Offset(0, Convert.ToInt32(Math.Ceiling((Convert.ToSingle(SystemInformation.BorderSize.Height) / 2f))));
            lineRight.Offset(0, Convert.ToInt32(Math.Ceiling((Convert.ToSingle(SystemInformation.BorderSize.Height) / 2f))));
            e.Graphics.DrawLine(forePen, lineLeft, lineRight);
        }
        forePen.Dispose();
        forePenDark.Dispose();
    }
}
